package com.example.hewei.smarttransport.Adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.example.hewei.smarttransport.Beans.CarState;
import com.example.hewei.smarttransport.R;


import java.util.List;

/**
 * Created by 何伟 on 2017/3/13.
 */

public class CarsStateGridAdapter extends ArrayAdapter {

    Context mContext;
    List<CarState> mDataArray;
    LayoutInflater layoutInflater;

    private static class ViewHolder {
        LinearLayout mBackGroundLayout;
        TextView mSpeedTV;
        TextView mCarIDTV;
        TextView mBalanceTV;
        TextView mLocationTV;
        Button mRechargeBT, mQueryBT;
        ToggleButton mToggle;
    }

    public CarsStateGridAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<CarState> objects) {
        super(context, resource, objects);
        mContext = context;
        mDataArray = objects;
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mDataArray.size();
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Nullable
    @Override
    public Object getItem(int position) {
        return mDataArray.get(position);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        final CarState mCarState = mDataArray.get(position);
        final ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.custom_gridview_item_car, parent, false);
            holder = new ViewHolder();
            holder.mBackGroundLayout = (LinearLayout) convertView.findViewById(R.id.background);
            holder.mSpeedTV = (TextView) convertView.findViewById(R.id.speed);
            holder.mCarIDTV = (TextView) convertView.findViewById(R.id.carID);
            holder.mBalanceTV = (TextView) convertView.findViewById(R.id.balance);
            holder.mLocationTV = (TextView) convertView.findViewById(R.id.location);
            holder.mRechargeBT = (Button) convertView.findViewById(R.id.recharge);
            holder.mQueryBT = (Button) convertView.findViewById(R.id.query);
            holder.mToggle = (ToggleButton) convertView.findViewById(R.id.toggle);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (mCarState != null) {
            holder.mToggle.setChecked(mCarState.isStart());
            holder.mCarIDTV.setText(mCarState.getCarID());
            holder.mSpeedTV.setText(mCarState.getSpeed());
            holder.mBalanceTV.setText(mCarState.getBalance());
            holder.mLocationTV.setText(mCarState.getLocation());
            holder.mToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        holder.mSpeedTV.setText("100");
                    } else {
                        holder.mSpeedTV.setText("0");
                    }
                    Toast.makeText(mContext, mCarState.getCarID() + "&" + isChecked, Toast.LENGTH_SHORT).show();
                }
            });
        }
        return convertView;
    }
}
